﻿#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 26 01:42:27 2019

@author: bing
"""


# 将检测结果进行形式转换：字符串转换成字典数据；同时，将关键数据b_box坐标数值化
def parse_result(predict_result):
    #result_dict = json.loads(predict_result) #在线模型结果直接是dict类型，不需要转换
    result_dict = predict_result
    # b_box_num = len(result_dict)
    # result_dict_new = {}
   
    # b_boxes = []
    # scores = []
    # class_name = []

    # for i in range(b_box_num):
    #
    #     brand_str = str(result_dict[i][0], encoding = "utf-8")
    #     class_name.append(brand_str)
    #
    #     # 直接获取 label 并添加到 class_name 列表中
    #     # brand_str = result_dict["label"]
    #     # class_name.append(brand_str)
    #
    #     scores.append(result_dict[i][1])
    #
    #    # center_x,center_y,width,height
    #     x1_min = int(result_dict[i][2][0] - result_dict[i][2][2]/2)
    #     y1_min = int(result_dict[i][2][1] - result_dict[i][2][3]/2) #left-top
    #
    #     x1_max = int(result_dict[i][2][0] + result_dict[i][2][2]/2)
    #     y1_max = int(result_dict[i][2][1] + result_dict[i][2][3]/2)
    #     b_boxes.append((x1_min,y1_min,x1_max,y1_max))
    #
    # result_dict_new["b_box_num"] = b_box_num
    # result_dict_new["detection_boxes"] = b_boxes
    # result_dict_new["detection_classes"] = class_name
    # result_dict_new["detection_scores"] = scores
    #
    # return result_dict_new


    # 初始化新的字典和列表
    result_dict_new = {}
    b_boxes = []
    class_name = []
    scores = []

    # 遍历每个结果
    for i in range(len(result_dict)):
        # 获取标签（label），直接访问字典的键
        brand_str = result_dict[i]["label"]  # 获取 label
        class_name.append(brand_str)  # 将 label 添加到 class_name 列表中

        # 获取置信度（confidence），直接访问字典的键
        scores.append(result_dict[i]["confidence"])  # 将 confidence 添加到 scores 列表中

        # 计算边界框的坐标（bbox），直接访问字典的键
        bbox = result_dict[i]["bbox"]
        x1_min = int(bbox["x"] - bbox["width"] / 2)
        y1_min = int(bbox["y"] - bbox["height"] / 2)  # 计算左上角坐标
        x1_max = int(bbox["x"] + bbox["width"] / 2)
        y1_max = int(bbox["y"] + bbox["height"] / 2)  # 计算右下角坐标
        b_boxes.append((x1_min, y1_min, x1_max, y1_max))  # 将边界框坐标添加到 b_boxes 列表中

    # 填充新的结果字典
    result_dict_new["b_box_num"] = len(result_dict)  # 检测框的数量
    result_dict_new["detection_boxes"] = b_boxes  # 检测框的坐标
    result_dict_new["detection_classes"] = class_name  # 检测到的类别
    result_dict_new["detection_scores"] = scores  # 检测的置信度

    return result_dict_new  # 返回处理后的结果字典


# 定义一个函数，根据车牌号查询比对车辆信息库（模拟车管所车辆登记信息库），判定是否是套牌车（车牌号和车辆品牌信息对应不上，后续可再增加车辆颜色信息）
def isFakePlate(inputCarInfo, carInfoDatabase):
    carBrandList = []
    isFakePlateCar = False
    trueCarBrand = ''
    plateNo = inputCarInfo[0]
    carBrand = inputCarInfo[1]
    if carBrand == '其他':
        isFakePlateCar = False
        trueCarBrand = 'Null'
    else:
        result = carInfoDatabase[(carInfoDatabase['plateNo']==plateNo)] # 从车管所数据库中拉出车牌号对应的车辆信息，保存到result中
        if len(result) > 0:
            carBrandList = result['carBrand'].values  # list结构 
           
            if (carBrand == carBrandList[0]):
                # print(carBrand, "==", carBrandList[0])
                isFakePlateCar = False
                trueCarBrand = carBrandList[0]
            else:
                # print(carBrand, "!=", carBrandList[0])
                isFakePlateCar = True
                trueCarBrand = carBrandList[0]
        else:
            isFakePlateCar = False
            trueCarBrand = 'Null'
    return isFakePlateCar, trueCarBrand

